package com.dingpeikeji.quickdeer.modules.base.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.SysAds;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SysAdsService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.google.common.collect.Lists;

/**
 * 用户通用界面
 */
@Controller
@RequestMapping("/f")
public class FCommonController {
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private SysAdsService sysAdsService;
	/**
	 * 三种类型的首页
	 */
	@RequestMapping("index{type}")
	public String index(@PathVariable("type") String type, Model model, String login, String register, HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		model.addAttribute("today", sdf.format(calendar.getTime())); // 当天日期
		calendar.add(Calendar.DAY_OF_YEAR, +1);
		model.addAttribute("tomorrow", sdf.format(calendar.getTime()));
		calendar.add(Calendar.MONTH, +3);
		String format = sdf.format(calendar.getTime());
		String specifiedDayBefore = getSpecifiedDayBefore(format);
		model.addAttribute("maxDay", specifiedDayBefore);
		model.addAttribute("time", "06:00");
		model.addAttribute("login", login); // 来自登录
		model.addAttribute("register", register); // 来自注册
		
		// 景区直通车景点中“上海迪斯尼”作为默认的目的站
		TSitedtl shanghaidisini = getSiteidByName("上海迪斯尼乐园");
		model.addAttribute("shanghaidisini", shanghaidisini);
		
		
		// 轮播图
		SysAds sysAdsQuery = new SysAds();
		sysAdsQuery.setPosition(1);
		// 排序
		Page<SysAds> page = new Page<SysAds>();
		page.setOrderBy("numsort");
		List<SysAds> sysAdsList =  sysAdsService.findList(sysAdsQuery);
		model.addAttribute("sysAdsList", sysAdsList);
		return "modules/base/index"+type;
	}
	
	public static String getSpecifiedDayBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}
	/**
	 * 活动页
	 */
	@RequestMapping("activity{type}")
	public String activity(@PathVariable("type") String type) {
		return "modules/base/activity"+type;
	}
	
	/**
	 * 支付完成页
	 */
	@RequestMapping("paySuccess")
	public String paySuccess() {
		return "modules/base/paySuccess";
	}
	
	
	/**
	 * 根据城市code异步获取景点列表
	 */
	@RequestMapping("json/getSiteData")
	@ResponseBody
	public List<TSitedtl> getSiteData(TSitedtl site) {
		String citycode = site.getCitycode();
		if(ValidateTool.isEmptyOrNull(citycode)) {
			return Lists.newArrayList();
		}
		return tSitedtlService.findList(site);
	}
	
	/**
	 * 根据定位城市名获取id
	 */
	@RequestMapping("json/getSiteidByName")
	@ResponseBody
	public TSitedtl getSiteidByName(String name) {
		TSitedtl site = new TSitedtl();
		site.setSitename(name.trim());
		List<TSitedtl> list = tSitedtlService.findList(site);
		if(list==null || list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}
}
